package donot996.icu;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Demo {

    /*
    Jsoup常用方法
    Document doc = Jsoup.parse(content); // 解析网页 得到文档对象
    doc.getElementsByTag("title"); // 获取tag是title的所有DOM元素
    doc.getElementById("nav_top");  // 获取id=nav_top的DOM元素
    doc.getElementsByClass("card"); // 根据样式名称来查询DOM元素
    doc.getElementsByAttribute("width"); // 根据属性名来查询DOM元素
    doc.getElementsByAttributeValue("target", "_blank"); // 根据属性名和属性值来查询DOM元素
    doc.select(".columns .column h1 a"); // 通过选择器查找DOM元素

    Element e = linkElements.first();
    e.text(); // 获取DOM元素文本
    e.attr("href"); //获取DOM元素属性值 */

    public static void main(String[] args) throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault(); // 创建httpclient实例
        HttpGet httpGet = new HttpGet("https://www.shicimingju.com/chaxun/list/4059.html"); // 创建httpGet实例
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36");
        CloseableHttpResponse response = httpClient.execute(httpGet); // 执行get请求
        HttpEntity entity = response.getEntity(); // 获取返回实体
        String content = EntityUtils.toString(entity, "utf-8"); // 获取网页内容
        response.close();

        Document doc = Jsoup.parse(content); // 解析网页 得到文档对象

        /*Elements linkElements = doc.select(".columns .column .card .card-content h1 a"); // 通过选择器查找所有博客的标题
        for (Element e : linkElements){
            System.out.println("博客标题；"+e.text()); // 获取文本内容
            System.out.println("博客链接："+e.attr("href")); // 获取特定属性值
        }*/
        Elements select = doc.select("#zs_content");
        for (Element element : select) {
            System.out.println(element.text());
        }
    }
}
